ORDERINGS = { 'ltbrecord' :	   { 'recsectitle' : ['source__title'], 
								'recsecauthor' : ['source__authors1'],
								'recprititle' : ['transcription__title'],
								'recpriauthor' : ['transcription__authors1'],										
									} ,

			'primarysource' : { 'prititle' : ['title'], 
								'pridate' : ['pubdate_from_year'],
								'priauthors' : ['authors1'],
								'prirepos' : ['repositories'],										
								} ,				
			}

DEFAULT_ORDERINGS = {	
						'restype' : 'ordering1', 
						'restype2' : 'ordering2', 
						'restype3' : 'ordering3', 
						}


def determine_ordering(result_type, ordering):
	""" maps a user-selected 'ordering_string' to the fields names (or list of) to be passed
	to the run_query (in FM) for ordering the result list  """

	reverse_flag = False

	if ordering.startswith('-'):
		ordering = ordering.replace("-", "")
		reverse_flag = True

	try:
		# this fails in some cases (eg when switching res type) so we use the exception!
		chosen_ordering = ORDERINGS[result_type][ordering]
	except:
		ordering = DEFAULT_ORDERINGS[result_type]	# by default
		chosen_ordering = ORDERINGS[result_type][ordering]

	if reverse_flag:
		return ['-' + x for x in chosen_ordering ]
	else:
		return chosen_ordering
